package pl.edu.mimuw.irs.rcp.view.figure;

import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.Shape;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.handles.HandleBounds;

/**
 * @author Jakub Rauch
 * Created on: 2009-05-26
 */
public class FigureWrapper extends Figure implements HandleBounds {
	
	// *** Attributes

	private Shape figure;
	private Label label;
	// Getters and settres
	public Shape getFigure() {
		return figure;
	}
	private void setFigure(Shape figure) {
		this.figure = figure;
	}
	public Label getLabel() {
		return label;
	}
	public void setLabel(Label label) {
		if (this.getLabel() != null) this.remove(getLabel());
		label.setTextPlacement(PositionConstants.SOUTH);
		if (label != null) add(label);
		setSize(getPreferredSize());
		this.label = label;
	}
	
	// *** Constructors
	
	public FigureWrapper(Shape wrappedFigure) {
		setFigure(wrappedFigure);
		//wrappedFigure.getBounds().crop(new Insets(5, 5, 5, 5));
		
		setLocation(wrappedFigure.getLocation().getCopy());
		
	    ToolbarLayout layout = new ToolbarLayout();
	    layout.setMinorAlignment(ToolbarLayout.ALIGN_TOPLEFT);
	    layout.setStretchMinorAxis(false);
	    layout.setSpacing(2);
	    layout.setVertical(true);
	    
	    setLayoutManager(layout);
	    setOpaque(true);
	    
	    add(wrappedFigure);

	}
	
	// *** Methods
	
	@Override
	protected void paintFigure(Graphics graphics) {
		graphics.setAlpha(0);
		super.paintFigure(graphics);
		graphics.setAlpha(255);
	}
	
	public void setLineWidth(int width) {
		getFigure().setLineWidth(width);
	}
	
	public Rectangle getHandleBounds() {
	    return getFigure().getBounds();
	}
} 
